from PyQt4.QtGui import QWidget,QPainter,QPen,QFont,QApplication,QVBoxLayout
from PyQt4.QtCore import QEvent, QSize,Qt,pyqtSignal,QUrl
from eventEngine import *
from PyQt4.QtWebKit import *
import webbrowser
import sys

class WebWidget(QWidget):
    def __init__(self):
        super(WebWidget, self).__init__()
        self.resize(280,600)
        self.verticalLayout = QVBoxLayout(self)
        self.webview = QWebView(self)
        self.webview = QWebView(self)
        # QWebPage.Notifications
        self.webview.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
        self.webview.settings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)
        self.webview.settings().setAttribute(QWebSettings.LocalStorageEnabled, True)
        self.webview.settings().setLocalStoragePath("/data")
        # self.webview.setContextMenuPolicy(Qt.NoContextMenu)
        self.webview.settings().setDefaultTextEncoding("utf-8")
        self.webview.setGeometry(1, 1, self.width() - 2, self.height() - 2)
        self.webview.setStyleSheet("QWebView{background-color: rgba(255, 193, 245, 0%); }")
        self.webview.page().networkAccessManager().setCookieJar(None)
        self.webview.page().mainFrame().javaScriptWindowObjectCleared.connect(self.setJavaScriptObject)
        self.webview.page().linkClicked.connect(self.linkClicked)
        # self.webview.page().featurePermissionRequested.connect(self.permissionRequested)

        self.webview.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.verticalLayout.addWidget(self.webview)

        self.webview.load(QUrl("http://webkit.org/"))
        # QWebPage.Notifications



    def setJavaScriptObject(self):
            self.webview.page().mainFrame().addToJavaScriptWindowObject("_window_", self)

    def linkClicked(self, url):
            webbrowser.open(url.toString())
def main():
    app = QApplication(sys.argv)
    mainwindow = WebWidget()
    mainwindow.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
        main()